package cn.alf.ssh.mlxt.action;

import java.util.HashSet;
import java.util.Set;

import javax.annotation.Resource;

import cn.alf.ssh.mlxt.domain.Address;
import cn.alf.ssh.mlxt.domain.User;
import cn.alf.ssh.mlxt.service.RegisterService;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {
	@Resource
	private RegisterService registerService;
	@Resource private User user;
	@Resource private Address address;
	private String name;
	private String password;
	private String repassword;
	private String addressString;
	private String email;
	private int age;

	public void setUser(User user) {
		this.user = user;
	}
    private String phone;
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getAddressString() {
		return addressString;
	}

	public void setAddressString(String addressString) {
		this.addressString = addressString;
	}

	public RegisterService getRegisterService() {
		return registerService;
	}

	public User getUser() {
		return user;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public void setRegisterService(RegisterService registerService) {
		this.registerService = registerService;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRepassword() {
		return repassword;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}


	public Address getAddress() {
		return address;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	public String execute() throws Exception {
		Set<Address > set = new HashSet<Address>();
		address.setLocation(this.addressString);
		address.setPhonenumber(this.phone);
		set.add(this.address);
		this.user.setName(this.name);
		this.user.setPassword(this.password);
		this.user.setAdds(set);
		this.user.setAge(this.age);
		this.user.setEmail(this.email);
		this.user.setGrade(1);
		boolean result =  this.registerService.registerUser(user);
		if(result){
			return "success";
		} else{
			this.addFieldError("name", "Register Error");
			return "input";
		}
	}
	
	@Override
	public void validate() {
		if(null == this.getName() || "".equals(this.getName().trim())){
			this.addFieldError("username", "username required");
		}
		if(null == this.getPassword() || "".equals(this.getPassword().trim())){
			this.addFieldError("password", "password required");
		}
	}

}
